<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学生学号，关联学生基础信息表" prop="studentId">
        <el-input
          v-model="queryParams.studentId"
          placeholder="请输入学生学号，关联学生基础信息表"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生姓名，冗余存储便于快速查询" prop="studentName">
        <el-input
          v-model="queryParams.studentName"
          placeholder="请输入学生姓名，冗余存储便于快速查询"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请提交日期" prop="applyDate">
        <el-date-picker clearable
          v-model="queryParams.applyDate"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择申请提交日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="一级审核人" prop="reviewer1">
        <el-input
          v-model="queryParams.reviewer1"
          placeholder="请输入一级审核人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="一级审核时间" prop="reviewer1Time">
        <el-date-picker clearable
          v-model="queryParams.reviewer1Time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择一级审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="一级审核结果" prop="reviewer1Result">
        <el-input
          v-model="queryParams.reviewer1Result"
          placeholder="请输入一级审核结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级审核人" prop="reviewer2">
        <el-input
          v-model="queryParams.reviewer2"
          placeholder="请输入二级审核人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二级审核时间" prop="reviewer2Time">
        <el-date-picker clearable
          v-model="queryParams.reviewer2Time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择二级审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="二级审核结果" prop="reviewer2Result">
        <el-input
          v-model="queryParams.reviewer2Result"
          placeholder="请输入二级审核结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级审核人" prop="reviewer3">
        <el-input
          v-model="queryParams.reviewer3"
          placeholder="请输入三级审核人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="三级审核时间" prop="reviewer3Time">
        <el-date-picker clearable
          v-model="queryParams.reviewer3Time"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择三级审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最终审核结果" prop="reviewer3Result">
        <el-input
          v-model="queryParams.reviewer3Result"
          placeholder="请输入最终审核结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['student:change:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['student:change:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['student:change:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['student:change:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="changeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="UUID主键，唯一标识每条学籍异动记录" align="center" prop="id" />
      <el-table-column label="学生学号，关联学生基础信息表" align="center" prop="studentId" />
      <el-table-column label="学生姓名，冗余存储便于快速查询" align="center" prop="studentName" />
      <el-table-column label="原学籍状态" align="center" prop="originalStatus" />
      <el-table-column label="异动类型" align="center" prop="changeType" />
      <el-table-column label="异动申请原因说明" align="center" prop="changeReason" />
      <el-table-column label="申请提交日期" align="center" prop="applyDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.applyDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请材料清单" align="center" prop="applyMaterials" />
      <el-table-column label="材料审核状态" align="center" prop="materialsStatus" />
      <el-table-column label="一级审核人" align="center" prop="reviewer1" />
      <el-table-column label="一级审核意见" align="center" prop="reviewer1Opinion" />
      <el-table-column label="一级审核时间" align="center" prop="reviewer1Time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewer1Time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="一级审核结果" align="center" prop="reviewer1Result" />
      <el-table-column label="二级审核人" align="center" prop="reviewer2" />
      <el-table-column label="二级审核意见" align="center" prop="reviewer2Opinion" />
      <el-table-column label="二级审核时间" align="center" prop="reviewer2Time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewer2Time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="二级审核结果" align="center" prop="reviewer2Result" />
      <el-table-column label="三级审核人" align="center" prop="reviewer3" />
      <el-table-column label="三级审核意见" align="center" prop="reviewer3Opinion" />
      <el-table-column label="三级审核时间" align="center" prop="reviewer3Time" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.reviewer3Time, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最终审核结果" align="center" prop="reviewer3Result" />
      <el-table-column label="最终学籍状态" align="center" prop="finalStatus" />
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['student:change:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['student:change:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学籍异动申请对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="changeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学生学号，关联学生基础信息表" prop="studentId">
          <el-input v-model="form.studentId" placeholder="请输入学生学号，关联学生基础信息表" />
        </el-form-item>
        <el-form-item label="学生姓名，冗余存储便于快速查询" prop="studentName">
          <el-input v-model="form.studentName" placeholder="请输入学生姓名，冗余存储便于快速查询" />
        </el-form-item>
        <el-form-item label="异动申请原因说明" prop="changeReason">
          <el-input v-model="form.changeReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="申请提交日期" prop="applyDate">
          <el-date-picker clearable
            v-model="form.applyDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择申请提交日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请材料清单" prop="applyMaterials">
          <el-input v-model="form.applyMaterials" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="一级审核人" prop="reviewer1">
          <el-input v-model="form.reviewer1" placeholder="请输入一级审核人" />
        </el-form-item>
        <el-form-item label="一级审核意见" prop="reviewer1Opinion">
          <el-input v-model="form.reviewer1Opinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="一级审核时间" prop="reviewer1Time">
          <el-date-picker clearable
            v-model="form.reviewer1Time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择一级审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="一级审核结果" prop="reviewer1Result">
          <el-input v-model="form.reviewer1Result" placeholder="请输入一级审核结果" />
        </el-form-item>
        <el-form-item label="二级审核人" prop="reviewer2">
          <el-input v-model="form.reviewer2" placeholder="请输入二级审核人" />
        </el-form-item>
        <el-form-item label="二级审核意见" prop="reviewer2Opinion">
          <el-input v-model="form.reviewer2Opinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="二级审核时间" prop="reviewer2Time">
          <el-date-picker clearable
            v-model="form.reviewer2Time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择二级审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="二级审核结果" prop="reviewer2Result">
          <el-input v-model="form.reviewer2Result" placeholder="请输入二级审核结果" />
        </el-form-item>
        <el-form-item label="三级审核人" prop="reviewer3">
          <el-input v-model="form.reviewer3" placeholder="请输入三级审核人" />
        </el-form-item>
        <el-form-item label="三级审核意见" prop="reviewer3Opinion">
          <el-input v-model="form.reviewer3Opinion" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="三级审核时间" prop="reviewer3Time">
          <el-date-picker clearable
            v-model="form.reviewer3Time"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择三级审核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最终审核结果" prop="reviewer3Result">
          <el-input v-model="form.reviewer3Result" placeholder="请输入最终审核结果" />
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Change">
import { listChange, getChange, delChange, addChange, updateChange } from "@/api/student/change";

const { proxy } = getCurrentInstance();

const changeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    studentId: null,
    studentName: null,
    originalStatus: null,
    changeType: null,
    changeReason: null,
    applyDate: null,
    applyMaterials: null,
    materialsStatus: null,
    reviewer1: null,
    reviewer1Opinion: null,
    reviewer1Time: null,
    reviewer1Result: null,
    reviewer2: null,
    reviewer2Opinion: null,
    reviewer2Time: null,
    reviewer2Result: null,
    reviewer3: null,
    reviewer3Opinion: null,
    reviewer3Time: null,
    reviewer3Result: null,
    finalStatus: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询学籍异动申请列表 */
function getList() {
  loading.value = true;
  listChange(queryParams.value).then(response => {
    changeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    studentId: null,
    studentName: null,
    originalStatus: null,
    changeType: null,
    changeReason: null,
    applyDate: null,
    applyMaterials: null,
    materialsStatus: null,
    reviewer1: null,
    reviewer1Opinion: null,
    reviewer1Time: null,
    reviewer1Result: null,
    reviewer2: null,
    reviewer2Opinion: null,
    reviewer2Time: null,
    reviewer2Result: null,
    reviewer3: null,
    reviewer3Opinion: null,
    reviewer3Time: null,
    reviewer3Result: null,
    finalStatus: null,
    remark: null
  };
  proxy.resetForm("changeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加学籍异动申请";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getChange(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改学籍异动申请";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["changeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateChange(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addChange(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除学籍异动申请编号为"' + _ids + '"的数据项？').then(function() {
    return delChange(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('student/change/export', {
    ...queryParams.value
  }, `change_${new Date().getTime()}.xlsx`)
}

getList();
</script>
